package com.feige.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feige.pojo.Role;
import com.feige.dao.RoleMapper;
import com.feige.pojo.RolePermission;
import com.feige.pojo.dto.RoleFormDto;
import com.feige.pojo.dto.RoleQueryDto;
import com.feige.service.RolePermissionService;
import com.feige.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feige.service.UserRoleService;
import com.feige.utils.string.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author feige
 * @since 2020-07-20
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private UserRoleService userRoleService;

    @Resource
    private RolePermissionService rolePermissionService;

    @Override
    public List<Role> selectRoleByUserId(Long userId) {
        List<Long> roleId = userRoleService.getRoleId(userId);
        return baseMapper.selectBatchIds(roleId);
    }

    @Override
    public IPage<Role> queryListByRoleQueryDto(RoleQueryDto roleQueryDto) {
        Page<Role> pageParam = new Page<>(roleQueryDto.getPageNum(), roleQueryDto.getPageSize());
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        if(!org.springframework.util.StringUtils.isEmpty(roleQueryDto.getRoleName())) {
            wrapper.like("role_name", roleQueryDto.getRoleName());
        }
        if (!org.springframework.util.StringUtils.isEmpty(roleQueryDto.getStatus())){
            wrapper.eq("status", roleQueryDto.getStatus());
        }
        if (!org.springframework.util.StringUtils.isEmpty(roleQueryDto.getBeginTime())){
            wrapper.ge("gmt_create", roleQueryDto.getBeginTime());
        }
        if (!org.springframework.util.StringUtils.isEmpty(roleQueryDto.getEndTime())){
            wrapper.le("gmt_create", roleQueryDto.getEndTime());
        }
        return this.page(pageParam, wrapper);
    }

    @Override
    public Set<String> selectRoleNameByUserId(Long userId) {
        return baseMapper.selectRoleNameByUserId(userId);
    }

    @Override
    public Set<String> selectAllRoleName() {
        return baseMapper.selectAllRoleName();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveRole(RoleFormDto roleFormDto) {
        Role role = new Role();
        BeanUtils.copyProperties(roleFormDto,role);
        boolean save = save(role);
        boolean assignPermissions = rolePermissionService.AssignPermissions(role.getId(), roleFormDto.getMenuIds());
        return save && assignPermissions;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateRole(RoleFormDto roleFormDto) {
        Role role = new Role();
        BeanUtils.copyProperties(roleFormDto,role);
        if (StringUtils.isNotNull(roleFormDto.getMenuIds()) && roleFormDto.getMenuIds().length != 0){
            QueryWrapper<RolePermission> wrapper = new QueryWrapper<>();
            wrapper.eq("role_id",role.getId());
            boolean remove = rolePermissionService.remove(wrapper);
            if (!remove){
                return false;
            }
        }
        boolean save = updateById(role);
        boolean assignPermissions = rolePermissionService.AssignPermissions(role.getId(), roleFormDto.getMenuIds());
        return save && assignPermissions;
    }

    @Override
    public List<Long> listRoleIdByUserId(Long userId) {
        return this.baseMapper.selectRoleIdByUserId(userId);
    }
}
